package com.tx.axure.tkaxureweb.controller;

import com.tx.axure.tkaxureweb.annotation.JspPage;
import com.tx.axure.tkaxureweb.annotation.LoginRequired;
import com.tx.axure.tkaxureweb.domain.entity.SProd;
import com.tx.axure.tkaxureweb.domain.entity.SProdGroup;
import com.tx.axure.tkaxureweb.domain.entity.SProdType;
import com.tx.axure.tkaxureweb.mapper.SProdGroupMapper;
import com.tx.axure.tkaxureweb.mapper.SProdMapper;
import com.tx.axure.tkaxureweb.mapper.SProdTypeMapper;
import com.tx.axure.tkaxureweb.vo.ResultVoStr;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/prod_group")
public class ProdGroupController {
    private final SProdTypeMapper sProdTypeMapper;
    private final SProdGroupMapper sProdGroupMapper;
    private final SProdMapper sProdMapper;

    /**
     * 1添加原型 2添加交互稿
     *
     * @return
     */
    @LoginRequired
    @JspPage
    @GetMapping("create/{type}")
    public String to_create(@PathVariable Integer type, HttpServletRequest request) {
        request.getParameter("userName");
        if (3 == type.intValue()) {
            return "other_create";
        }
        List<SProdGroup> prodGroupList = sProdGroupMapper.select(SProdGroup.builder().sts(true).build());
        request.setAttribute("prodGroupList", prodGroupList);
        return "prodGroup/create";
    }

    /**
     * 新增产品组
     *
     * @param sProdGroup
     * @return
     */
    @PostMapping("do_create")
    @LoginRequired
    public @ResponseBody
    ResultVoStr doCreate(SProdGroup sProdGroup) {
        sProdGroup.setCreateTime(new Date());
        sProdGroup.setSts(true);
        sProdGroupMapper.insertSelective(sProdGroup);
        return new ResultVoStr();
    }

    /**
     * 修改产品组
     *
     * @param sProdGroup
     * @return
     */
    @PostMapping("do_edit")
    @LoginRequired
    public @ResponseBody
    ResultVoStr doEdit(SProdGroup sProdGroup) {
        sProdGroup.setUpdateTime(new Date());
        sProdGroup.setSts(true);
        sProdGroupMapper.updateByPrimaryKeySelective(sProdGroup);
        return new ResultVoStr();
    }

    /**
     * 修改产品组
     *
     * @param groupdId
     * @return
     */
    @GetMapping("do_delete/{groupdId}")
    @LoginRequired
    public @ResponseBody
    ResultVoStr doDelete(@PathVariable Integer groupdId) {
        SProdGroup sProdGroup = SProdGroup.builder()
                .id(groupdId)
                .sts(false).build();
        //判断一下产品组下面是否存在产品，存在则不允许删除
        SProd sProdQuery=SProd.builder()
                        .groupId(groupdId)
                                .sts(true).build();
        if(sProdMapper.selectCount(sProdQuery)>0){
            return new ResultVoStr(10001,"分组下存在目录资源，请先删除！");

        }
        sProdGroupMapper.updateByPrimaryKeySelective(sProdGroup);
        return new ResultVoStr();
    }

    /**
     * 验签算法，返回true表示验签通过
     *
     * @param timeStamp 时间戳，从http header中获取
     * @param nonce     随机数，从http header中获取
     * @param userId    用户标识，从http header中获取
     * @param uamToken  用户标识，包含回调URL路径中，可通过request.getParameter方法获得
     * @param signStr   签名串，从http header中获取
     * @param appSecret 应用的密钥信息，三方应用备案通过之后由运营方颁发获得
     * @return
     */
    public static boolean validateSignature(String timeStamp, String nonce, int userId, String uamToken, String signStr, String appSecret) {
        return false;
    }

    /**
     * 跳转到产品group列表页
     *
     * @return
     */
    @LoginRequired
    @GetMapping("list")
    public String list(HttpServletRequest request) {
        List<SProdType> prodTypeList = sProdTypeMapper.selectAll();
        request.setAttribute("prodTypeList", prodTypeList);
        List<SProdGroup> prodGroupList = sProdGroupMapper.select(SProdGroup.builder().sts(true).build());
        request.setAttribute("prodGroupList", prodGroupList);
        return "prod_group";
    }
}
